package cn.cocho.custom.yunCashierDf.news.service;

import cn.cocho.common.web.character.DesEncrypt;
import cn.cocho.common.web.character.StringUtils;
import cn.cocho.common.web.jqgrid.JQGridPage;
import cn.cocho.common.web.jqgrid.JQGridUtils;
import cn.cocho.custom.yunCashierDf.news.domain.NewsInfo;
import cn.cocho.custom.yunCashierDf.news.domain.NewsModule;
import cn.cocho.custom.yunCashierDf.utils.AppConstants;
import cn.cocho.dborm.core.Dborm;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by WQJ on 2015/11/12.
 */
@Service("newsInfoService")
public class NewsInfoService {

    public JQGridPage getListByJQgrid(JQGridPage<NewsInfo> pageJQGrid) {
        pageJQGrid = JQGridUtils.getListByJQgrid(pageJQGrid, NewsInfo.class, "news_info");
        for (NewsInfo newsInfo : pageJQGrid.getDataRows()) {
             newsInfo.init();
        }
        return pageJQGrid;
    }

    public JQGridPage getModuleListByJQgrid(JQGridPage<NewsModule> pageJQGrid) {
        pageJQGrid = JQGridUtils.getListByJQgrid(pageJQGrid, NewsModule.class, "viewAllNews");
        for (NewsModule newsModule : pageJQGrid.getDataRows()) {
            newsModule.init();
        }
        return pageJQGrid;
    }

    public void update(NewsInfo newsInfo) {
        Dborm.replace(newsInfo);
    }

    public void add(NewsInfo newsInfo) {
        newsInfo.setNewsId(StringUtils.getUUID());
        Dborm.insert(newsInfo);
    }

    public NewsInfo getNewsInfoById(String newsId) {
        NewsInfo newsInfo = new NewsInfo();
        newsInfo.setNewsId(newsId);
        return Dborm.getEntityByExample(newsInfo, true);
    }

    public boolean insertNewsInfo(NewsInfo newsInfo) {
        newsInfo.setNewsId(StringUtils.getUUID());
        return Dborm.insert(newsInfo);
    }

    public boolean replaceNewsInfo(NewsInfo newsInfo) {
        newsInfo.setNewsId(newsInfo.getNewsId());
        return Dborm.replace(newsInfo);
    }

    public List<NewsInfo> getNewsInfo() {
        String sql = "SELECT * FROM news_info ORDER BY last_modify_time desc";
        return Dborm.getEntities(sql, null, NewsInfo.class);
    }
}
